/*package com.cdfahe.common.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import net.heartsome.xml.Document;
import net.heartsome.xml.Element;
import net.heartsome.xml.SAXBuilder;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Component;
import org.xml.sax.SAXException;

import com.cdfahe.gms.service.ICommonService;

*//**
 * 通用搜索组件
 * @author Roy
 *
 *//*
//@Component
public class SearchUtils { 
	
	public SearchUtils(ICommonService iCommonService) {
		this.iCommonService = iCommonService;
	}


	public static String PATH = "resources/search-config.xml";
	
	Element root = null;
	
	private Element getRoot(){
		if (root !=null){
			return root;
		} else {
			root =  getDoc(PATH).getRootElement();
			return root;
		}
	}
	
	public Element getSearchConditonsByName(String search){
		List<Element> searchList = getRoot().getChildren();
		for (Element e: searchList){
			if (e.getAttribute("id").getValue().equals(search)){
				return e;				
			}
		}
		return null;
	}

    private ICommonService iCommonService;
	
	*//**
	 * 根据search-config.xml中SearchSearchCondition下的compare节点的值获得比较符的集合
	 * @param compare
	 * @return
	 *//*
	public static String[] getCompares(String compare){
		switch (Integer.parseInt(compare)) {
		case 1:
			return SearchCondition.COMPARE_1;
		case 2:
			return SearchCondition.COMPARE_2;
		case 3:
			return SearchCondition.COMPARE_3;
		default:
			return new String[]{};
		}
	} 
	
	public JSONObject get(HttpServletRequest request) {
//		String sColumns = "check,langname,langcode,iscommonused,id";
		String iSortCol_0 = request.getParameter("iSortCol_0");
		String sSortDir_0 = request.getParameter("sSortDir_0");
//		String sSearch = request.getParameter("sSearch");
		String sEcho = request.getParameter("sEcho");
		String start = request.getParameter("iDisplayStart");
		String length = request.getParameter("iDisplayLength");
		String orderby = "";
		
		
		// 需要从界面中获取的参数,请karl补全
		String search=request.getParameter("node"); // 当前界面的search节点的名称
		String[] selFileds  = request.getParameter("columns").split(","); // 用户在界面上选中的要显示的字段
		if (iSortCol_0 != null && !"0".equals(iSortCol_0)) {
			orderby = " order by " + selFileds[Integer.parseInt(iSortCol_0) - 1].substring(selFileds[Integer.parseInt(iSortCol_0) - 1].indexOf(".") + 1) 
					+ " " + sSortDir_0 + " ";
		}
		// 需要从界面中获取的参数
		
		// 按页数查询指定数量的的数据
		String fieldsql = getSearchConditonsByName(search).getChild("sql").getText();		
		String conditions = getSqlWhere(request);		
		String finalsql = fieldsql + conditions + orderby + " limit "+start +","+length ;
		SQLAdapter ada = new SQLAdapter();
		ada.setSequels(finalsql);
		List<Map<String,String>> listData = iCommonService.getDataBySql(ada);
		
		// 查询符合条件的数据的条数
		String totalSql = getSearchConditonsByName(search).getChild("countsql").getText();	
		totalSql += conditions;
		ada.setSequels(totalSql);
		List<Map<String, String>> lstTotal = iCommonService.getDataBySql(ada);
		Object total = lstTotal.get(0).get("TOTAL");	
		
		List<SearchCondition> list = getSearchConditions(search);
		JSONObject json = new JSONObject();
		JSONArray array = new JSONArray();
		for (Map<String,String> t : listData) {
			Object[] obj = new Object[selFileds.length + 2];
			obj[0] = "";
			for (int i=0;i<selFileds.length;i++){
				obj[i + 1] = t.get(selFileds[i].substring(selFileds[i].lastIndexOf(".") + 1));
				if (obj[i + 1] == null) {
					obj[i + 1] = "";
				} else {
					obj[i + 1] = String.valueOf(obj[i + 1]);
					if ("project".equals(search) && !"".equals(String.valueOf(obj[i + 1]))) {
						if (selFileds[i].equals("TG_PRJ_PROJECT.PUBLICTM") || selFileds[i].equals("TG_PRJ_PROJECT.SELFTM") || selFileds[i].equals("TG_PRJ_PROJECT.SUBJECTID")) {
							for (SearchCondition sc : list) {
								if (selFileds[i].equals(sc.getName())) {
									String name = "";
									Map<String, String> map = sc.getMapValues();
									for (String s : String.valueOf(obj[i + 1]).split(",")) {
										if (map.get(s) != null) {
											name += "," + map.get(s);
										}
									}
									obj[i + 1] = ("".equals(name) ? "" : name.substring(1));
								}
							}
						}
					}
				}
			}	
			obj[selFileds.length+1] = t.get("ID");
			array.add(obj);
		}
		
		json.put("iTotalRecords", String.valueOf(total));
		json.put("iTotalDisplayRecords", String.valueOf(total));
		json.put("sEcho", sEcho);
		json.put("sColumns", "");
		json.put("aaData", array);
		
		return json;
	}
	
	
	
	*//**
	 * 
	 * 根据前台传回的搜索条件和界面名称得到sql语句
	 * @param lstcond
	 * @param search
	 * @return
	 *//*
	public String getSql(List<SearchCondition> lstcond, String search){		
		String conditions = getSqlWhere(lstcond, search);
		String fieldsql = getSearchConditonsByName(search).getChild("sql").getText();
		return fieldsql + conditions;
	}
	
	public String getSqlWhere(HttpServletRequest request) {
		String conditions = "";
		String fields = request.getParameter("fields");
		if (fields != null && !"".equals(fields.trim())) {
			String[] field = fields.split(",");
			for (String f : field) {
				String cb = request.getParameter("cb_" + f);
				String operators = request.getParameter("operators_" + f);
				String values = request.getParameter("values_" + f);
				if("contains".equals(operators)) {
					conditions += " and " + cb + " like '%" + values + "%'";
				} else if ("not contains".equals(operators)) {
					conditions += " and " + cb + " not like '%" + values + "%'";
				} else if ("is".equals(operators)) {
					conditions += " and " + cb + " in (" + values + ")";
				} else if ("is not".equals(operators)) {
					conditions += " and " + cb + " not in (" + values + ")";
				} else if ("in".equals(operators)) {
					String values1 = request.getParameter("values_start" + f);
					String values2 = request.getParameter("values_end" + f);
					if (values1 != null && !"".equals(values1)) {
						conditions += " and " + cb + ">= '"+ values1 + "'";
					}
					if (values2 != null && !"".equals(values2)) {
						conditions += " and " + cb + "<= '"+ values2 + "'";
					}
				}
			}
		}
		return conditions;
	}
	
	public String getSqlWhere(List<SearchCondition> lstcond, String search){		
		String conditions = "";
		for (SearchCondition sc : lstcond){
			String cond ="and ";
			String dbfield = sc.getDbfield();
			String value = sc.getValue();
			String compare = sc.getSelectCompare();
			if (compare.equals("is")){
				if (sc.getIsMultSelect()){
					cond += dbfield +" in ("+value+ ")";
				} else {
					cond += dbfield + " = '"+value+"'";
				}
				
			} else if (compare.equals("is not")){
				if (sc.getIsMultSelect()){
					cond += dbfield + "not in ("+value+ ")";
				} else {
					cond += dbfield + "!= '"+value+"'";
				}
			}else if (compare.equals("contains")){
				cond += dbfield + "like '%"+value+"%'";
			} else if (compare.equals("not contains")){
				cond += dbfield + "not like '%"+value+"%'";
			} else if (compare.equals("in")){
				if (sc.getStartDate()!=null){
					cond += dbfield + ">= "+ sc.getStartDate();
				}
				if (sc.getEndDate()!=null){
					cond += dbfield + "<=" + sc.getEndDate();
				}
			}
			conditions += cond;
		}
		return  conditions;
	}
	
	
	*//**
	 * 返回搜索界面中选中和未选中的显示字段的数据
	 * @param search
	 * @return
	 *//*
	public Map<String,String[]> getField(String search){
		Map<String, String[]> result = new HashMap<String, String[]>();
		Element searchCondtions = getSearchConditonsByName(search);
		Element searchField = searchCondtions.getChild("SearchField");
		String[] selectfield = searchField.getChild("seletcfield").getText().split(",");
		result.put("select", selectfield);
		String[] unselectfield = searchField.getChild("unselectfield").getText().split(",");
		result.put("unselect", unselectfield);
		return result;
	}
	
	*//**
	 * 根据search-config.xml中Search节点的值获得SearchCondition的下拉列表的值
	 * @param search
	 * @return
	 *//*
	public List<SearchCondition> getSearchConditions(String search) {
		List<SearchCondition> lst = new ArrayList<SearchCondition>();

		Element e = getSearchConditonsByName(search);
		List<Element> lstSearchCondition = e.getChildren("SearchCondition");

		for (Element ec : lstSearchCondition) {
			SearchCondition c = new SearchCondition();
			c.setName(ec.getChild("name").getText());
			c.setDbfield(ec.getChild("dbfield").getText());
			c.setCompare(ec.getChild("compare").getText());
			c.setCompares(Arrays.asList(getCompares(c.getCompare())));

			String valueType = ec.getChild("valuetype").getText();
			String value = ec.getChild("value").getText();
			c.setValuetype(valueType);
			c.setValue(value);

			if (valueType.equals(SearchCondition.VALUETYPE_DYNAMICLIST)){
				Map<String,String> map = new LinkedHashMap<String, String>();
				SQLAdapter ada = new SQLAdapter();
				ada.setSequels(value);
				List<Map<String, String>> lstmap = iCommonService.getDataBySql(ada);
				for (Map<String, String> m : lstmap) {
					map.put(String.valueOf(m.get("VALUE")), m.get("NAME"));
				}
				c.setMapValues(map);
			} else if (valueType.equals(SearchCondition.VALUETYPE_STATICLIST)){
				String[] arrValue = value.split(",");
				Map<String,String> map = new HashMap<String, String>();
				for (int i=0;i<arrValue.length/2;i++){
					map.put(arrValue[2*i], arrValue[2*i+1]);
				}
				c.setMapValues(map);
			}
			lst.add(c);
		}
		return lst;
	}
	
	

	
	public static Document getDoc(String filepath){
		try {
			return new SAXBuilder().build(filepath);
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public String getFields(List<SearchCondition> list) {
		String fields = "";
		for (SearchCondition s : list) {
			fields += "," + s.getDbfield();
		}
		
		fields = "".equals(fields)?fields:fields.substring(1);
		return fields;
	}
	
}
*/